/**
* userCtrl - AngularJS module for right sidebar.
*
*
* Credits
* =======
*
* Copyright 2015 LonghronShen <LonghronShen@Hotmail.com>
*/



(function () {
    "use strict";

    /**
     * Config
     */
    var moduleName = 'app';
    var DEFAULT_ID = '__default';

    /**
     * Module
     */
    var module;
    try {
        module = angular.module(moduleName);
    } catch (err) {
        // named module does not exist, so create one
        module = angular.module(moduleName, []);
    }

    module.controller('ramyCtrl', ['$scope', '$rootScope', '$state', '$ngBootbox', 'dataService', 'valueservice', 'uiGridConstants', function ($scope, $rootScope, $state, $ngBootbox, dataService, valueservice, uiGridConstants) {
        // console.info("<userCtrl> Defining userCtrl...");
        window.localStorage.id = null;
        window.localStorage.carCode = null;

        $scope.queryParameter={
           Carcode : "苏A250",
            //searchingRoleId : "",
           DriverName : "529",
           PhoneNumber : "13737373838",
           MinRamyPoint : "",
           MaxRamyPoint : "",
        }


        

      

        var orderBy = $scope.orderBy = "";
        var acs = $scope.acs = 0;
        var skip = $scope.skip = 0;
        var count = $scope.count = 10;
        
        //角色
        $scope.roles = [];
        var str = [];

        //审核状态枚举
      
        //初始化列表
        var loadRamyData = function (queryParameter, useCache, orderBy,acs,skip, count) {
            dataService.getUsers(queryParameter.Carcode,queryParameter.DriverName,queryParameter.PhoneNumber,queryParameter.MinRamyPoint,queryParameter.MaxRamyPoint, useCache == null ? false : useCache, orderBy, acs,skip, count).then(function (res) {
                res = res.data || { Response: { Output: { List: [], Total: 0 } } };
                res.Response = res.Response || { Output: { List: [], Total: 0 } };
                res.Response.Output = res.Response.Output || { List: [], Total: 0 };
                
                var list = res.Response.Output.List;
           
                //$scope.gridApi.grid.options.totalItems = res.result.total;
                $scope.gridApi.grid.options.totalItems = res.Response.Output.Total;
            
                // $scope.ramyList = { data: list };
                var currentPage = $scope.gridApi.grid.options.paginationCurrentPage;
                var totalPages = $scope.gridApi.pagination.getTotalPages();
                if (totalPages < currentPage) { $scope.gridApi.grid.options.paginationCurrentPage = 1; };
                $scope.seeks = false;
            }, function (rej) {
                $ngBootbox.alert(rej);
                $scope.seeks = false;
            });
        };

        //分页初始化
        var paginationOptions = {
            pageNumber: 0,
            pageSize: 10,
            currentPage: 1,
            sort: null,
            useCache: true,

        };
        var ramyList = $scope.ramyList = {
            data: [],
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: false,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "手机号码", field: 'a', enableColumnMenu: false, },
                {displayName: "驾驶员姓名", field: 'b', enableColumnMenu: false},
                { displayName: "车牌号", field: 'c', enableColumnMenu: false },
                { displayName: "累计报价次数", field: 'd', enableColumnMenu: false,enableSorting: true },
                { displayName: "累计驾驶里程", field: 'e', enableColumnMenu: false,enableSorting: true },
                { displayName: "累积驾驶时间", field: 'f', enableColumnMenu: false ,enableSorting: true},
                //{ displayName: "用户名", field: 'UserName', enableColumnMenu: false },
                { displayName: '当前拉米积分', field: 'g', enableColumnMenu: false,enableSorting: true },
               
               
                
                {
                    displayName: "操作",
                    field: '操作',
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" data-target="#quoteModal" class="fa fa-eye m-l-xs primary text-info" title="查看" ng-click="grid.appScope.checkDetail(row.entity)"></a>',
                    enableColumnMenu: false
                }
            ],
            
            onRegisterApi: function (gridApi) {
                console.log(gridApi)
                $scope.gridApi = gridApi;
                $scope.gridApi.core.on.sortChanged($scope, $scope.sortChanged);
                $scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.OPTIONS);
                //翻页
                gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                    alert('hehe')
                    paginationOptions.pageNumber = newPage * pageSize - pageSize;
                    paginationOptions.pageSize = pageSize;
                    paginationOptions.currentPage = newPage;
                    $scope.gridApi.grid.options.paginationCurrentPage = newPage;
                    $scope.skip = paginationOptions.pageNumber;
                    $scope.count = paginationOptions.pageSize;
                    loadRamyData($scope.queryParameter,paginationOptions.useCache, $scope.orderBy, $scope.acs, paginationOptions.pageNumber, paginationOptions.pageSize);
                    paginationOptions.useCache = true;
                });
            }
        };
        //排序
        $scope.sortChanged = function (grid, sortColumns) {
            if (sortColumns.length === 0) {
                loadRamyData($scope.queryParameter,false, $scope.orderBy, $scope.acs, paginationOptions.pageNumber, paginationOptions.pageSize);
            } else {
                switch (sortColumns[0].sort.direction) {
                    case "asc":
                        $scope.orderBy = sortColumns[0].name;
                        $scope.acs = 2;
                        loadRamyData($scope.queryParameter,false, $scope.orderBy, $scope.acs, paginationOptions.pageNumber, paginationOptions.pageSize);
                        break;
                    case "desc":
                        $scope.orderBy = sortColumns[0].name;
                        $scope.acs = 1;
                        loadRamyData($scope.queryParameter,false, $scope.orderBy, $scope.acs, paginationOptions.pageNumber, paginationOptions.pageSize);
                        break;
                    case undefined:
                        loadRamyData($scope.queryParameter,false, $scope.orderBy, $scope.acs, paginationOptions.pageNumber, paginationOptions.pageSize);
                        break;
                }
            };
        };

        //初次记载数据
        // loadRamyData($scope.queryParameter,true, $scope.orderBy, $scope.acs, paginationOptions.pageNumber, paginationOptions.pageSize);

        $scope.seeks = false;
        //查询调用
        $scope.queryUsers = function () {
           
            $scope.seeks = true;
            paginationOptions.pageNumber = 0;
            paginationOptions.currentPage = 1;
            paginationOptions.useCache = false;
            loadRamyData($scope.queryParameter,false, '', 0, paginationOptions.pageNumber, paginationOptions.pageSize);
        };


        $scope.ramyList.data=[{'a':'2016-1-30 14:00:00','b':'1上海上海静安区','c':'江苏南京浦口区','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'大米','g':'20吨','h':'9.6米'},
                                {'a':'2016-2-30 14:00:00','b':'2htshsaergsth','c':'sjsert','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'gwe','g':'13吨','h':'9.6米'},
                                {'a':'2016-3-30 14:00:00','b':'3jdusrtyj','c':'usrtjxfghx','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'ghh','g':'13吨','h':'9.6米'},
                                {'a':'2016-4-30 14:00:00','b':'4htrsjyss','c':'thaq45ytrujyi','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'jjkj','g':'14吨','h':'9.6米'},
                                {'a':'2016-5-30 14:00:00','b':'5srtjytjdjaa','c':'sthxfgjhkk','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'qaqe4y','g':'9.2吨','h':'9.6米'},
                                {'a':'2016-6-30 14:00:00','b':'6srtstyk','c':'artyhsrtujhyij','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'ay','g':'3.6吨','h':'9.6米'},
                                {'a':'2016-7-30 14:00:00','b':'7aeatrsyjk','c':'gegreqerqgt5r','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'aq','g':'0.36吨','h':'9.6米'},
                                {'a':'2016-8-30 14:00:00','b':'8rehtjrthsatgesr','c':'jyjkstarasgrt','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'saty','g':'5.6吨','h':'9.6米'},
                                {'a':'2016-9-30 14:00:00','b':'8aethayrjyar','c':'artjjiaet','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'th','g':'15吨','h':'9.2米'},
                                {'a':'2016-10-30 14:00:00','b':'10aehaergea','c':'jykar5a','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'hqa','g':'15吨','h':'9.6米'},
                                {'a':'2016-11-30 14:00:00','b':'11aejtjiarth','c':'ae5turkjjns','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'ya','g':'15吨','h':'9.6米'},
                                {'a':'2016-12-30 14:00:00','b':'12aerhayhja','c':'haerhaeha','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'uwr','g':'15吨','h':'9.6米'},
                                {'a':'2016-11-12 14:00:00','b':'13aetyjarth','c':'aeruikyka','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'at5y','g':'15吨','h':'9.6米'},
                                {'a':'2016-11-16 14:00:00','b':'14tauyikzaert','c':'atrjahaW','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'a5y','g':'15吨','h':'9.6米'},
                                {'a':'2016-11-26 14:00:00','b':'15erahyjjkk','c':'AERHAHAZR','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'a5y','g':'15吨','h':'9.6米'},
                                {'a':'2016-11-10 14:00:00','b':'16aer5yae','c':'RESHSTRJhsdthjsr','d':'2016-11-30 16:00:00','e':'2016-12-01 17:00：00','f':'r6u','g':'15吨','h':'9.6米'},
                                ];








        //弹框的表格--------------------------------------------------------------------------
        
        var ramyPointList = $scope.ramyPointList = {
            data: [],
            paginationPageSizes: [10],
            useExternalPagination: true,
            useExternalSorting: false,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "记录时间", field: 'a', enableColumnMenu: false, width:'33%',enableSorting:true},
                {
                    displayName: "积分变动", field: 'b', enableColumnMenu: false, cellTooltip: 
                true, headerTooltip: true, enableSorting: true,width:'33%'
                },
                { displayName: "说明", field: 'c', enableColumnMenu: false, width:'33%' },
               
            ],
            
            onRegisterApi: function (gridApi) {
                $scope.detailGridApi = gridApi;
               
                $scope.detailGridApi.core.notifyDataChange(uiGridConstants.dataChange.OPTIONS);
                $scope.detailGridApi.core.on.sortChanged($scope, $scope.detailSortChanged);
                gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                    paginationOptions.pageNumber = newPage * pageSize - pageSize;
                    paginationOptions.pageSize = pageSize;
                    paginationOptions.currentPage = newPage;
                    $scope.gridApi.grid.options.paginationCurrentPage = newPage;
                    $scope.skip = paginationOptions.pageNumber;
                    $scope.count = paginationOptions.pageSize;
                    loadDetail('time',paginationOptions.useCache, $scope.detail.orderBy, $scope.detail.acs, paginationOptions.pageNumber, paginationOptions.pageSize);
                    paginationOptions.useCache = true;
                });
            }
        };
        $scope.ramyPointList.data=$scope.ramyList.data;
        //加载拉米积分明细
        $scope.detailSortChanged=function(){

        }

        var loadDetail = function (parameter) {
            //这里不一定需要调用接口。 先丢这里吧
        };




      

       
        

    }]);
})();